using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TunderController : MonoBehaviour
{
    [SerializeField] private StatCharacter targetStat;
    [SerializeField] private float speed;
    private Animator animator;
    private int damage;
    bool triggered;
    private void Awake()
    {
        animator = GetComponentInChildren<Animator>();
    }
    private void Update()
    {
        MoveToTarget();
    }
    private void MoveToTarget()
    {
        if (triggered) return;
        if (!targetStat) return;
        transform.position = Vector2.MoveTowards(transform.position, targetStat.transform.position, speed * Time.deltaTime);
        transform.right = transform.position - targetStat.transform.position;
        if (Vector2.Distance(transform.position, targetStat.transform.position) < .1f)
        {
            animator.transform.localRotation = Quaternion.identity;
            transform.localRotation = Quaternion.identity;
            transform.localScale = new Vector3(3, 3);
            triggered = true;
            animator.SetTrigger("Hit");
            targetStat.TakeDamage(damage);
            targetStat.CreateShock(true);
            Destroy(gameObject, .5f);
        }
    }
    public void Setup(StatCharacter targetStat, int damage)
    {
        this.targetStat = targetStat;
        this.damage = damage;
    }
}
